<?php
class SearchController extends AppController {
	var $name = 'Search';
	var $uses = array();
	var $components = array('Menu','Keyword');

	function beforeFilter() {
		$_session = $this -> Session -> read();
		$this->loadModel('Hitcounter');
		$row = $this->Hitcounter->get(session_id());
		if ($row == 0) {
			$data = array(
				"session_id" => session_id(),
				"ref_url" => env("REQUEST_URI"),
				"ip_address" => env("REMOTE_ADDR"),
				"activity" => date("Y-m-d H:i:s")
			);
			$this->Hitcounter->save($data);
		}
		$_menus = $this->Menu->getMenu($this->params['controller'],$this->params['action']);
		for ($i = 1; $i <= 6; $i++) {
			if ($_menus[$i-1] == 1) {
				$this->set('current'.$i,'class="current"');
			}
			else {
				$this->set('current'.$i,'');
			}
		}

		$this->Keyword->startup($this);
		$this->Keyword->get();
	}

	function index() {
		$this->layout = 'inside';

		if (!$this->Session->read("lang")) {
			$lang = "vie";
		} else {
			$lang = $this->Session->read("lang");
		}

		Configure::write("Config.language", $lang);

		$this->loadModel('Product');
		$key = strip_tags($this->params['url']['keyword']);
		//echo $key;

		if ($key != "") {
			// tim trong content
			$products = $this->Product->find('all', array(
				'conditions' => array(
					'or' => array(
						"name_{$lang} like" => "%{$key}%",
						" description_{$lang} like" => "%{$key}%",
						" model like" => "%{$key}%",
					)
				)
			));

			if (empty($products))  {
				$this->set('result','Không tìm thấy kết quả nào.');
			}
			else {
				//$count = count($res) + count($res2);
				$this->set('result','Đã tìm thấy '.count($products).' kết quả');
				$this->set('products',$products);
			}
		}
		$this->set('lang',$lang);
	}
}
?>